home *** CD-ROM | disk | FTP | other *** search
- /* MModal.c */
- /* The simplest C System 7 shell application */
- /* Modified to show a simple Moveable Modal window */
-
- /* This shell can be very handy for debugging and testing things */
- /* Add menu items, add dialogs, add controls, or whatever else you need */
- /* This sample contains basic application startup and event loop handling, */
- /* add more features as your needs increase. */
- /* This sample is High Level Event aware, so you can send and receive AppleEvents */
- /* from this application */
- /* Written by C.K. Haun <TR> */
- /* Apple Developer Tech Support */
- /* October 1991, Tokyo, Japan */
- /* Of course, Copyright 1991-1992, Apple Computer Inc. */
-
-
- #include <Types.h>
- #include <memory.h>
- #include <Packages.h>
- #include <Errors.h>
- #include <quickdraw.h>
- #include <fonts.h>
- #include <dialogs.h>
- #include <windows.h>
- #include <menus.h>
- #include <events.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <diskinit.h>
- #include <OSUtils.h>
- #include <resources.h>
- #include <toolutils.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <GestaltEqu.h>
- #include <PPCToolbox.h>
- #include <Processes.h>
- #include <Balloons.h>
- #include <ALiases.h>
- #include <Processes.h>
- #include <StandardFile.h>
- #include <Folders.h>
-
- /* prototypes */
- void OpenMovable(void);
- void CloseMovable(void);
- void InitalizeApp(void);
- void DoDiskEvents(long dinfo); /* hi word is error code, lo word is drive number */
- void DrawMain(WindowPtr drawIt);
- void DrawMain2(WindowPtr drawIt);
- void DrawMovable(WindowPtr drawIt);
- Boolean DoSelected(long val);
- void InitAEStuff(void);
- void DoHighLevel(EventRecord *AERecord);
- void DoDaCall(MenuHandle themenu, long theit);
- pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- void SampleHelpDialog(void);
- void SetModalButtonState(gInBackground);
-
- /* one external */
- extern void _DataInit(); /* this is the C initialization code */
-
- enum {
- kMBarID = 128
- };
- enum {
- kAppleMenu = 128, kFileMenu, kEditMenu, kToolsMenu
- };
- enum {
- kResumeMask = 1, /* bit of message field for resume vs. suspend */
- kSampHelp = 129, kAboutBox = 128, kHelpString = 128, kNewItem = 1, kOpenItem, kCloseItem, kSaveItem, kSaveAsItem, kFileBlank1,
- kPageSetupItem, kPrintItem, kFileBlank2, kQuitItem, kBadSystem = 130, kGenStrings = 128, kMenuToggleStrings = 129,
- kCloseButton = 128, kMyModalKind = 1000, kMyDocumentKind
- };
- enum {
- kItem1 = 1
- };
- enum {
- kCloseIt = 1, kOpenIt
- };
- struct AEinstalls {
- AEEventClass theClass;
- AEEventID theEvent;
- EventHandlerProcPtr theProc;
- };
- typedef struct AEinstalls AEinstalls;
-
- /* some globals */
- MenuHandle gAppleMenuHandle, gFileMenuHandle, gEditMenuHandle, gToolMenuHandle;
- Boolean gQuit, gInBackground;
- EventRecord gERecord;
- Point gSavedPoint;
- Boolean gSavedPos;
- AEDesc gTheAddress;
- ProcessSerialNumber gOurSN;
- short gHelpItem;
- /* this is our global to tell us if a movable modal is up*/
- Boolean gModalUp = false;
- unsigned long gMySleep = 30;
-
- #pragma segment Main
- main()
- {
- WindowPtr twindow;
- UnloadSeg((Ptr)_DataInit); /* throw out setup code */
- InitalizeApp();
- UnloadSeg((Ptr)InitalizeApp); /* get rid of my initialization code */
- do {
-
- WaitNextEvent(everyEvent, &gERecord, gMySleep, nil);
- switch (gERecord.what) {
- ProcPtr drawProc;
- short tempKind;
- case nullEvent:
- /* no nul processing in this sample */
- break;
-
- case updateEvt:
- tempKind = ((WindowPeek)gERecord.message)->windowKind;
- /* Mkae sure it's my window before I jump through the refCon */
- /* Why, since DA's have they're own layer in 7.0? */
- /* BECAUSE there are other people in the universe who will */
- /* add things to your windowList.BalloonWriter, for example, */
- /* so you still need to be careful */
- if (tempKind == kMyModalKind || tempKind == kMyDocumentKind) {
- /* get the drawing proc from the refCon */
- drawProc = (ProcPtr)GetWRefCon((WindowPtr)gERecord.message);
- /* jump to it */
- drawProc((WindowPtr)gERecord.message);
- }
- break;
-
- case mouseDown:
- /* first see where the hit was */
- /* When you first think about the Movable Modal issue, you may*/
- /* say to yerself "Hey!if there's a mouseDown all I have to do is*/
- /* see if it's in the portRect of the front window, and SysBeep if it isn't!" */
- /* Nah, too easy.Remember, you want a user to be able to switch out */
- /* of your application when a movable modal is up, so you still need */
- /* to call FindWindow and see if it was a hit in the desktop, other*/
- /* app window (same as desktop), or in the Help or Application menus. */
- /* So, we just check our gModalUp flag in any place where */
- /* window positions may shift, and sysbeep elsewhere */
-
- switch (FindWindow(gERecord.where, &twindow)) {
-
- case inDesk: /* if they hit in desk, then the process manager */
- break; /* will switch us out, we don't need to do anything */
-
- case inMenuBar:
- /* let Help and Application menus happen always */
- DoSelected(MenuSelect(gERecord.where));
- break;
-
- case inSysWindow:
- /* pass to the system */
- SystemClick(&gERecord, twindow);
- break;
-
- case inContent:
- /* Handle content and control clicks here */
- if (twindow != FrontWindow()) {
- if (!gModalUp) {
- SelectWindow(twindow); /* select the window */
- SetPort(twindow);
- } else {
- SysBeep(1); /* complain because they hit a back window with MM up */
- }
- } else {
- /* control tracking or whatever.In this simple sample I */
- /* only have one control possible, so deal with it */
- Point locPoint = gERecord.where;
- ControlHandle returnedControl;
- GlobalToLocal(&locPoint);
- if (FindControl(locPoint, twindow, &returnedControl)) {
- if (TrackControl(returnedControl, locPoint, nil)) {
- CloseMovable();
- }
- }
- }
- break;
-
- case inDrag:
- if (twindow != FrontWindow() && gModalUp) {
- /* don't do anything, can't drag a back window */
- SysBeep(1);
- } else {
- DragWindow(twindow, gERecord.where, &qd.screenBits.bounds);
- }
- break;
-
- case inGrow:
- /* Call GrowWindow here if you have a grow box */
- break;
-
- case inGoAway:
- /* Click in Close box */
- break;
- }
- case mouseUp:
- /* don't care */
- break;
- /* same action for key or auto key */
- case keyDown:
- case autoKey:
- if (gERecord.modifiers & cmdKey)
- DoSelected(MenuKey(gERecord.message & charCodeMask));
- break;
-
- case keyUp:
- /* don't care */
- break;
-
- case diskEvt:
- /* I don't do anything special for disk events, this just passes them */
- /* to a function that checks for an error on the mount */
- DoDiskEvents(gERecord.message);
- break;
-
- case activateEvt:
- /* not doing anything on activates */
- break;
-
- case networkEvt:
- /* don't care */
- break;
-
- case driverEvt:
- /* don't care */
- break;
-
- case app4Evt:
- /* If we're switching layers and I have my MModal up, I want to */
- /* change the state of the control */
- switch ((gERecord.message >> 24) & 0x0FF) { /* high byte of message */
-
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (gERecord.message & kResumeMask) == 0;
- /* set dim/enable on the control */
- SetModalButtonState(gInBackground);
- break;
-
- }
- break;
-
- /* This dispatches high level events (AppleEvents, for example) */
- /* to our dispatch routine.This is NEW in the event loop for */
- /* System 7 */
- case kHighLevelEvent:
- DoHighLevel(&gERecord);
- break;
- default:
- break;
-
- }
- }
- while (gQuit != true);
- }
-
- /* DoDaCall opens the requested DA.It's here as a seperate routine if you'd */
- /* like to perform some action or just know when a DA is opened in your */
- /* layer.Can be handy to track memory problems when a DA is opened */
- /* with an Option-open */
- void DoDaCall(MenuHandle themenu, long theit)
- {
- long qq;
- char DAname[255];
- GetItem(themenu, theit, &DAname);
- qq = OpenDeskAcc(DAname);
- }
-
- /* end DoDaCall */
-
- /* DoDiskEvents just checks the error code from the disk mount, */
- /* and puts up the 'Format' dialog (through DIBadMount) if need be */
- /* You can do much more here if you care about what disks are */
- /* in the drive */
- void DoDiskEvents(long dinfo) /* hi word is error code, lo word is drive number */
- {
- short hival, loval, tommy;
- Point fredpoint = {
- 40, 40
- };
- hival = HiWord(dinfo);
- loval = LoWord(dinfo);
- if (hival != noErr) /* something happened */ {
- tommy = DIBadMount(fredpoint, dinfo);
- }
- }
-
- /* draws my window.Pretty simple */
- void DrawMain(WindowPtr drawIt)
- {
- Handle theText;
- WindowPtr tempWP;
- GetPort(&tempWP);
- BeginUpdate(drawIt);
- SetPort(drawIt);
- EraseRect(&drawIt->portRect);
- theText = GetResource('TEXT', 128);
- if (theText) {
- Rect textRect;
- HLock(theText);
- HUnlock(theText);
- textRect = drawIt->portRect;
- InsetRect(&textRect, 20, 20);
- TextBox(*theText, GetHandleSize(theText), &textRect, 0);
- ReleaseResource(theText);
- }
- EndUpdate(drawIt);
- SetPort(tempWP);
- }
- /* a second version for variety */
- void DrawMain2(WindowPtr drawIt)
- {
- Handle theText;
- WindowPtr tempWP;
- GetPort(&tempWP);
-
- BeginUpdate(drawIt);
- SetPort(drawIt);
- EraseRect(&drawIt->portRect);
- theText = GetResource('TEXT', 130);
- if (theText) {
- Rect textRect;
- HLock(theText);
- HUnlock(theText);
- textRect = drawIt->portRect;
- InsetRect(&textRect, 20, 20);
- TextBox(*theText, GetHandleSize(theText), &textRect, 0);
- ReleaseResource(theText);
- }
- EndUpdate(drawIt);
- SetPort(tempWP);
- }
-
- /* Draws the Movable Modal window contents */
- void DrawMovable(WindowPtr drawIt)
- {
- WindowPtr tempWP;
- Handle theText;
- BeginUpdate(drawIt);
- GetPort(&tempWP);
-
- SetPort(drawIt);
- EraseRect(&drawIt->portRect);
- theText = GetResource('TEXT', 129);
- if (theText) {
- Rect textRect;
- HLock(theText);
- HUnlock(theText);
- textRect = drawIt->portRect;
- InsetRect(&textRect, 20, 20);
- TextBox(*theText, GetHandleSize(theText), &textRect, 0);
- ReleaseResource(theText);
- }
- DrawControls(drawIt);
- EndUpdate(drawIt);
- SetPort(tempWP);
- }
-
- /* my menu action taker.It returns a Boolean which I usually ignore, but it */
- /* mught be handy someday */
- Boolean DoSelected(long val)
- {
- short loval, hival;
- Boolean returnVal = false;
- loval = LoWord(val);
- hival = HiWord(val);
-
- switch (hival) { /* switch off the menu number selected */
- case kAppleMenu: /* Apple menu */
- if (loval != 1) { /* if this was not About, it's a DA */
- DoDaCall(gAppleMenuHandle, loval);
- } else {
- Alert(kAboutBox, nil); /* do about box */
- }
- returnVal = true;
- break;
- case kFileMenu: /* File menu */
- switch (loval) {
- case kQuitItem:
- gQuit = true; /* onlyitem */
- returnVal = true;
- break;
- default:
- break;
- }
- break;
-
- case kEditMenu:
- /* edit menu junk */
- /* don't care */
- break;
-
- case kToolsMenu:
- /* add all your test stuff here */
- if (gModalUp)
- CloseMovable();
- else
- OpenMovable();
- break;
-
- case kHMHelpMenuID: /* Defined in Balloons.h */
- /* I only care about this item.If anything else is returned here, I don't know what */
- /* it is, so I leave it alone.Remember, the Help Manager chapter says that */
- /* Apple reserves the right to add and change things in the Help menu */
- if (loval == gHelpItem)
- SampleHelpDialog();
- break;
-
- }
- HiliteMenu(0);
- return(returnVal);
- }
-
- /* InitAEStuff installs my appleevent handlers */
- void InitAEStuff(void)
- {
- static AEinstalls HandlersToInstall[] = { {
- kCoreEventClass, kAEOpenApplication, AEOpenHandler
- }, {
- kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
- }, {
- kCoreEventClass, kAEQuitApplication, AEQuitHandler
- }, {
- kCoreEventClass, kAEPrintDocuments, AEPrintHandler
- },
- /* The above are the four required AppleEvents. */
-
- };
-
- OSErr aevtErr = noErr;
- long aLong = 0;
- Boolean gHasAppleEvents = false;
- /* Check this machine for AppleEvents.If they are not here (ie not 7.0)
- * then we exit */
- gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
- /* The following series of calls installs all our AppleEvent Handlers.
- * These handlers are added to the application event handler list that
- * the AppleEvent manager maintains.So, whenever an AppleEvent happens
- * and we call AEProcessEvent, the AppleEvent manager will check our
- * list of handlers and dispatch to it if there is one.
- */
- if (gHasAppleEvents) {
- register qq;
- for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
- aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
- HandlersToInstall[qq].theProc, 0, false);
- if (aevtErr) {
- ExitToShell(); /* just fail, baby */
- }
- }
- } else {
- ExitToShell();
- }
- }
-
- /* end InitAEStuff */
- /* I'm not doing error handling in this sample for clarities sake, you should. Hah, */
- /* easy for me to say, huh? */
- void DoHighLevel(EventRecord *AERecord)
- {
-
- AEProcessAppleEvent(AERecord);
-
- }
-
- /* end DoHighLevel */
-
- /* This is the standard Open Application event.*/
- pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,reply,refIn)
- Str255 theName;
- WindowPtr myWindow, secondWindow;
- /* we of course don't do anything here in this simple app */
- /* except open our window */
- myWindow = GetNewWindow(128, nil, (WindowPtr)-1);
- ((WindowPeek)myWindow)->windowKind = kMyDocumentKind;
- /* install drawing proc */
- SetWRefCon(myWindow, (long)DrawMain);
- /* open another one, so we can demonstrate shuffling inhibiting */
- secondWindow = GetNewWindow(128, nil, (WindowPtr)-1);
- ((WindowPeek)secondWindow)->windowKind = kMyDocumentKind;
- /* install drawing proc */
- SetWRefCon(secondWindow, (long)DrawMain2);
- GetIndString(theName, kGenStrings, 1);
- SetWTitle(secondWindow, theName);
- SelectWindow(myWindow);
- SetPort(myWindow);
- return(noErr);
-
- }
-
- /* end AEOpenHandler */
-
- /* Open Doc, opens our documents.Remember, this can happen at application start AND */
- /* anytime else.If your app is up and running and the user goes to the desktop, hilites one */
- /* of your files, and double-clicks or selects Open from the finder File menu this event */
- /* handler will get called. Which means you don't do any initialization of globals here, or */
- /* anything else except open then doc.*/
- /* SO-- Do NOT assume that you are at app start time in this */
- /* routine, or bad things will surely happen to you. */
-
- pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,refIn,reply)
- /* we of course don't do anything here */
- return(errAEEventNotHandled); /* we have no docs, so no odoc events should come to us */
- }
-
- pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- { /* no printing handler in yet, so we'll ignore this */
- /* the operation is functionally identical to the ODOC event, with the additon */
- /* of calling your print routine.*/
- #pragma unused (messagein,refIn,reply)
- /* we of course don't do anything here */
- return(errAEEventNotHandled); /* we have no docs, so no pdoc events should come to us */
- }
-
- /* Standard Quit event handler, to handle a Quit event from the Finder, for example.*/
- /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.*/
- pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,refIn,reply)
-
- /* prepQuit sets the Stop flag for us.It does _NOT_ quit, you */
- /* should NEVER quit from an AppleEvent handler.Calling */
- /* ExitToShell here would blow things up */
- gQuit = true;
- return(noErr);
- }
-
- /* This is my sample help dialog.Does not do anything, expand as you need */
- void SampleHelpDialog(void)
- {
- DialogPtr tdial = GetNewDialog(kSampHelp, nil, (WindowPtr)-1);
- short itemhit = 0;
- while (itemhit != 1) {
- ModalDialog((ModalFilterProcPtr)nil, &itemhit);
- }
- DisposDialog(tdial);
- }
-
-
- /* Opens the MModal dialog. */
- /* Just for grins, I'm also saving and restoring it's position in*/
- /* a very simple way */
- void OpenMovable(void)
- {
- WindowPtr temp;
- Str63 theWords;
- temp = GetNewWindow(129, nil, (WindowPtr)-1);
- /* install drawing proc */
- SetWRefCon(temp, (long)DrawMovable);
-
- /* set the kind to my MModalwindow */
- ((WindowPeek)temp)->windowKind = kMyModalKind;
- gModalUp = true;
-
- /* add the go away button */
- GetNewControl(kCloseButton, temp);
-
- /* change the menu item text */
- GetIndString(theWords, kMenuToggleStrings, kCloseIt);
- SetItem(gToolMenuHandle, kItem1, theWords);
-
- if (gSavedPos) {
- /* move it to the saved position */
- /* move it to 0,0 to avoid any math at all */
- MoveWindow(temp, 0, 0, false);
- MoveWindow(temp, gSavedPoint.h, gSavedPoint.v, false);
- }
- ShowWindow(temp);
- /* kill all the menus except the Test, Help, and App */
- DisableItem(gAppleMenuHandle, 0);
- DisableItem(gFileMenuHandle, 0);
- DisableItem(gEditMenuHandle, 0);
- DrawMenuBar();
- SetPort(temp);
- }
-
- void CloseMovable(void)
- {
- Str63 theWords;
- WindowPtr temp = FrontWindow();
- /* the front window really should be my modal window */
- /* if it isn't, I'm bailing.*/
- gSavedPos = true;
- gSavedPoint.h = temp->portRect.left;
- gSavedPoint.v = temp->portRect.top;
- LocalToGlobal(&gSavedPoint);
- if (((WindowPeek)temp)->windowKind == kMyModalKind)
- CloseWindow(temp);
- else
- gQuit = true;
-
- gModalUp = false;
-
- /* reset the menu item text */
- GetIndString(theWords, kMenuToggleStrings, kOpenIt);
- SetItem(gToolMenuHandle, kItem1, theWords);
- /* turn our menus back on */
- EnableItem(gAppleMenuHandle, 0);
- EnableItem(gFileMenuHandle, 0);
- EnableItem(gEditMenuHandle, 0);
- DrawMenuBar();
- }
-
- /* Dim the MModal button when we go into the background */
- void SetModalButtonState(gInBackground)
- {
- WindowPtr theWindow = FrontWindow();
- ControlHandle theControl;
- short hState;
- if (gModalUp) {
- /* only if we exist, o' course */
- if (((WindowPeek)theWindow)->windowKind == kMyModalKind) {
- /* if this isn't true, then things are weird */
- hState = (gInBackground ? 255 : 0);
- theControl = ((WindowPeek)theWindow)->controlList;
- HiliteControl(theControl, hState);
-
- }
- }
- }
-
-
- #pragma segment Initialize
- void InitalizeApp(void)
- {
- MenuHandle helpHandle;
- Handle myMenu;
- StringHandle helpString;
- short count;
- long vers;
- MaxApplZone();
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- /* Check system version */
- Gestalt(gestaltSystemVersion, &vers);
- vers = (vers >> 8) & 0xf; /* shift result over and mask out major version number */
- if (vers < 7) {
- StopAlert(kBadSystem, nil);
- ExitToShell();
- }
- InitAEStuff();
- /* set up my menu junk */
- myMenu = GetNewMBar(kMBarID);
- SetMenuBar(myMenu);
- gAppleMenuHandle = GetMHandle(kAppleMenu);
- gFileMenuHandle = GetMHandle(kFileMenu);
- gEditMenuHandle = GetMHandle(kEditMenu);
- gToolMenuHandle = GetMHandle(kToolsMenu);
- AddResMenu(gAppleMenuHandle, 'DRVR');
- /* now install my Help menu item in the Help Manager's menu */
- HMGetHelpMenuHandle(&helpHandle); /* Get the Hlpe menu handle */
- count = CountMItems(helpHandle); /* How many items are there? */
- helpString = GetString(kHelpString); /* get my help string */
- DetachResource(helpString); /* detach it */
- HNoPurge(helpString);
- MoveHHi((Handle)helpString);
- HLock((Handle)helpString);
- InsMenuItem(helpHandle, (Ptr)*helpString, count + 1); /* insert my item in the Help menu */
- gHelpItem = CountMItems(helpHandle); /* The number of the item */
-
- DrawMenuBar();
- GetCurrentProcess(&gOurSN); /* Get our process serial number for later use, if needed */
-
- }
-